
layui.use(['form', 'layedit', 'laydate',"table",'upload'], function() {
    var $ = layui.jquery
        , upload = layui.upload;
    var table = layui.table;
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;

    //日期
    laydate.render({
        elem: '#searchStartTime'
    });
    laydate.render({
        elem: '#searchEndTime'
    });
    //监听工具条
    table.on('tool(demo)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            layer.msg('ID：'+ data.id + ' 的查看操作');
        } else if(obj.event === 'del'){
            layer.confirm('确定要封印此账号吗?', function(index){
                $.ajax({
                    url:"/userController/sealUser",
                    type:"PUT",
                    data:{id:data.id},
                    success:function(data,status,xhr){
                        if (xhr.code == -1){
                            top.location.href="/pageController/login"

                        }
                        if(xhr.status ==204){
                            layer.alert('封印成功', {icon: 1}, function(index){
                                layer.closeAll("page");
                                layer.close(index);
                                var curr = sessionStorage.getItem("curr");
                                table.reload('testReload',{page:{curr:curr}});
                            });
                        }else{
                            layer.alert('操作失败', {icon: 5});
                        }
                    }
                })
                layer.close(index);

            });
        } else if(obj.event === 'edit'){
            layer.confirm('确定要解除封印吗?', function(index){
                $.ajax({
                    url:"/userController/unSealUser",
                    type:"PUT",
                    data:{id:data.id},
                    success:function(data,status,xhr){
                        if (xhr.code == -1){
                            top.location.href="/page/login"

                        }
                        if(xhr.status == 204){
                            layer.alert('解封成功', {icon: 1}, function(index){
                                layer.closeAll("page");
                                layer.close(index);
                                var curr = sessionStorage.getItem("curr");
                                table.reload('testReload',{page:{curr:curr}});
                            });
                        }else{
                            layer.alert('操作失败', {icon: 5});
                        }
                    }
                })
                layer.close(index);

            });


        }
    });

    //方法级渲染
    table.render({
        elem: '#table'
        ,url: '/userController/paramlist'
        ,cols: [
            [
                {type:'numbers', title: '序',align:"center"},
                { field: 'id', title: '用户ID',align:"center"},

                { field: 'nickName', title: '昵称',align:"center"},

                { field: 'mobile', title: '手机号',align:"center"},

                { field: 'unionId', title: '微信绑定',align:"center",templet: function(d){
                    if(isNull(d.unionId)){
                        return "--";
                    }else {
                        return "是"
                    }

                }},

                { field: 'workNumber', title: '作品数',align:"center",templet: function(d){
                    if(isNull(d.workNumber)){
                        return "--";
                    }else {
                        return d.workNumber
                    }

                }},

                { field: 'fansNumber', title: '粉丝数',align:"center",templet: function(d){
                    if(isNull(d.fansNumber)){
                        return "--";
                    }else {
                        return d.fansNumber
                    }

                }},


                {field: 'createTime', title: '注册时间',align:"center",templet: function(d){
                    if(isNull(d.createTime)){
                        return "-";
                    }
                    return getFormatDateByLong(d.createTime, "yyyy-MM-dd hh:mm");
                }},
                {field: null , title: '操作',align:"center",templet:"#barDemo" }
            ]
        ]
        ,id: 'testReload'
        ,page: true
        ,done:function (res, curr, count) {
            sessionStorage.setItem("curr",curr)

        }
    });

    var $ = layui.$, active = {
        reload: function(){
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    name: $("#selectname").val(),
                    startTime: $('input[name="startTime"]').val(),
                    endTime:$('input[name="endTime"]').val()
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });


});



